///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.data.handlers.flex.remoting {
    import mx.logging.*;
    import mx.logging.ILogger;
    
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.AbstractOperation;
    import mx.rpc.remoting.mxml.RemoteObject;
    
    import mx.utils.ObjectUtil;
    
    import flash.events.EventDispatcher;
    import flash.events.Event;
    
    import sugarcookie.data.handlers.flex.remoting.events.RemotingServiceResultEvent;
    
    public class RemotingServiceHandler extends EventDispatcher {
    	private var _log:ILogger;
    	private var _remoteObject:RemoteObject;
    	private var _destination:String;
    	private var _eventType:String;
    	private var _parent:RemotingServiceManager;
    	private var _operation:AbstractOperation;
    	public static const RESULT_EVENT:String = "resultEvent";
    	public static const FAULT_EVENT:String = "faultEvent";
    	
    	public function RemotingServiceHandler(p:RemotingServiceManager,dest:String,src:String):void {
    		_parent = p;
    		_remoteObject = new RemoteObject(dest);
    		_remoteObject.source = src;
            _destination = dest;
            init();
    	}
    	
    	private function init():void {
			_log = Log.getLogger("sugarcookie.dataHandlers.RemotingServiceHandler");
			
    		_remoteObject.addEventListener("result", handleResults);
            _remoteObject.addEventListener("fault", handleFault);
    	}
    	
    	public function get eventType():String {
    		return _eventType;
    	}
    	
    	public function set dest(d:String):void {
    		_remoteObject.destination = _destination = d;
    	}
    	
    	public function get dest():String {
    		return _destination;
    	}
    	
    	public function set busyCursor(value:Boolean):void {
    		_remoteObject.showBusyCursor = value;
    	}
    	
    	public function call(method:String,evtType:String=RemotingServiceHandler.RESULT_EVENT,...args:Array):void {
			_log.debug("call (method: "+method+", evtType: "+evtType+", args: "+args+")");
			_eventType = evtType;
			
    		_operation = _remoteObject[method];
			_operation.send(args);
            if(args.length >0){
				_operation.send(args);
            }  else {
                 _log.debug("No arguments we're received.");
                 _operation.send();
            }
    	}
    	
    	private function handleResults(evt:ResultEvent):void {
			_log.debug("handleResults.");
			_parent.lastResult = evt.result as Object;
			_parent.dispatchResult(new RemotingServiceResultEvent(_eventType,false,false,_destination));
			
			_operation.removeEventListener(ResultEvent.RESULT, handleResults);
            _operation.removeEventListener(FaultEvent.FAULT, handleFault);
        }
 
        private function handleFault(evt:FaultEvent):void {
        	_parent.lastResult = evt.fault;
			_parent.dispatchResult(new RemotingServiceResultEvent(_eventType,false,false,_destination));
        }
    }
}




